# -*- coding:utf-8 -*—
#!/usr/bin/python
# -*- coding:utf-8 -*-
# @Time    :2021/12/19 17:41
# @Author   : 亮哥
# @Mail    : yxl20160@126.com
# @File      : excel_transfrom_json.py
# Software  : PyCharm
import os

import pandas as pd
import json

def json_inputs(open_path):
    """
    返回json字符串列表
    :param path: 需要转换excel文件的路径
    :return: 返回json列表
    """
    df = pd.read_excel(open_path)
    # print(df)
    cols = [colName for colName in df.columns]
    json_list = []
    for row in df.itertuples():
        json_dict = {}
        for index in range(len(cols)):
            json_dict[cols[index]] = getattr(row,cols[index])
        json_list.append(json_dict)

    return json_list

def save_json(open_path):
    json_list = json_inputs(open_path)
    exportPath = os.path.split(os.path.abspath(open_path))[0]
    outputPath = exportPath + './transfrom.json'
    with open(outputPath,"w",encoding="utf-8") as fw:
        #解决中文编码问题
        json.dump(json_list,fw,ensure_ascii=False)


if __name__ == '__main__':
    # open_path = r'C:\Users\tslilove\Desktop\haha.xlsx'
    # open_path = r'F:\json\json转excel.xlsx'
    open_path = r"F:\data\水电流域\2022.09.26预报数据对比\9月预报数据对比\9月份实测\实时9月份.xlsx"
    # save_path = 'transfrom.json'
    save_json(open_path)
    print("----完成----")
